package com.yss.sofa.report.engine.util;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;

public final class CssUtil {
	public static String font(CellStyle style,float zoom,Sheet sheet){
		StringBuilder css=new StringBuilder();
		String fontcolor="#000";
        if(style instanceof XSSFCellStyle){
    		if(((XSSFCellStyle)style).getFont().getColor()==0){
    			byte[] rgb=((XSSFCellStyle)style).getFont().getXSSFColor().getRGBWithTint();
    			fontcolor="rgb("+(rgb[0] & 0xff)+","+(rgb[1] & 0xff)+","+(rgb[2] & 0xff)+")";
    		}
    		css.append("color:").append(fontcolor).append(";");
    		css.append("font-family:").append("'").append(((XSSFCellStyle)style).getFont().getFontName()).append("'").append(";");
    		css.append("font-size:").append((((XSSFCellStyle)style).getFont().getFontHeight()*zoom/20)).append("pt").append(";");
    		css.append("font-style:").append(((XSSFCellStyle)style).getFont().getItalic()?"italic":"normal").append(";");
    		css.append("font-weight:").append(((XSSFCellStyle)style).getFont().getBold()?"bold":"normal").append(";");
    		css.append("text-decoration:").append(((XSSFCellStyle)style).getFont().getStrikeout()?"line-through":"inherit").append(";");
        }else if(style instanceof HSSFCellStyle){
        	if(((HSSFCellStyle)style).getFont(sheet.getWorkbook()).getColor()==0){
        		fontcolor=((HSSFCellStyle)style).getFont(sheet.getWorkbook()).getHSSFColor((HSSFWorkbook) sheet.getWorkbook()).getHexString();
 
    		}
    		css.append("color:").append(fontcolor).append(";");
    		css.append("font-family:").append("'").append(((HSSFCellStyle)style).getFont(sheet.getWorkbook()).getFontName()).append("'").append(";");
    		css.append("font-size:").append((((HSSFCellStyle)style).getFont(sheet.getWorkbook()).getFontHeight()*zoom/20)).append("pt").append(";");
    		css.append("font-style:").append(((HSSFCellStyle)style).getFont(sheet.getWorkbook()).getItalic()?"italic":"normal").append(";");
    		css.append("font-weight:").append(((HSSFCellStyle)style).getFont(sheet.getWorkbook()).getBold()?"bold":"normal").append(";");
    		css.append("text-decoration:").append(((HSSFCellStyle)style).getFont(sheet.getWorkbook()).getStrikeout()?"line-through":"inherit").append(";");
        }
		return css.toString();
	}
	public static String transform(CellStyle style){
		StringBuilder css=new StringBuilder();
		short rotate=style.getRotation();
		if(rotate!=0){
			css.append("transform:rotate(").append(rotate).append("deg);");
			css.append("-ms-transform:rotate(").append(rotate).append("deg);");
			css.append("-moz-transform:rotate(").append(rotate).append("deg);");
			css.append("-webkit-transform:rotate(").append(rotate).append("deg);");
			css.append("-o-transform:rotate(").append(rotate).append("deg);");
		}
		return css.toString();
	}
	public static String background(CellStyle style){
		StringBuilder css=new StringBuilder();
		String bgcolor="#fff";
		if(style.getFillForegroundColor()==0){
			if(style instanceof XSSFCellStyle){
				byte[] rgb=((XSSFCellStyle)style).getFillForegroundColorColor().getRGBWithTint();
				bgcolor="rgb("+(rgb[0] & 0xff)+","+(rgb[1] & 0xff)+","+(rgb[2] & 0xff)+")";
			}else if(style instanceof HSSFCellStyle){
				short[] rgb = ((HSSFCellStyle)style).getFillForegroundColorColor().getTriplet();
				if(rgb!=null) bgcolor="rgb("+(rgb[0] & 0xff)+","+(rgb[1] & 0xff)+","+(rgb[2] & 0xff)+")";
			}
		}
		css.append("background-color:").append(bgcolor).append(";");
		return css.toString();
	}
	public static String wordBreak(CellStyle style){
		StringBuilder css=new StringBuilder();
		if(style.getWrapText()){
			css.append("word-wrap:break-word;word-break:break-all;");
		} else {
			css.append("white-space:nowrap;word-break:keep-all;");
		}
		return css.toString();
	}
	public static String align(CellStyle style){
		StringBuilder css=new StringBuilder();
		switch (style.getAlignmentEnum()) {
		case CENTER:
		case LEFT:
		case RIGHT:
		case JUSTIFY:
			css.append("text-align:").append(style.getAlignmentEnum().toString()).append(";");
			break;
		case CENTER_SELECTION:
			css.append("text-align:").append("CENTER").append(";");
			break;
		case DISTRIBUTED:
		case FILL:
		case GENERAL:
			css.append("text-align:").append("LEFT").append(";");
			break;
		default:
			break;
		}
		switch (style.getVerticalAlignmentEnum()) {
		case JUSTIFY:
		case DISTRIBUTED:
		case CENTER:
			css.append("vertical-align:").append("MIDDLE").append(";");
			break;
		case BOTTOM:
		case TOP:
			css.append("vertical-align:").append(style.getVerticalAlignmentEnum().toString()).append(";");
			break;
		default:
			break;
		}
		return css.toString();
	}
	private static String borderStyle(BorderStyle border){
		String style="none";
		switch (border) {
		case NONE:
			style="none";
			break;
		case THIN:
			style="solid";
			break;
		case MEDIUM:
			style="solid";
			break;
		case DASHED:
			style="dashed";
			break;
		case DOTTED:
			style="dotted";
			break;
		case THICK:
			style="solid";
			break;
		case DOUBLE:
			style="double";
			break;
		case HAIR:
			style="dotted";
			break;
		case MEDIUM_DASHED:
			style="dashed";
			break;
		case DASH_DOT:
			style="dashed";
			break;
		case MEDIUM_DASH_DOT:
			style="dashed";
			break;
		case DASH_DOT_DOT:
			style="dashed";
			break;
		case MEDIUM_DASH_DOT_DOT:
			style="dashed";
			break;
		case SLANTED_DASH_DOT:
			style="dashed";
			break;
		default:
			break;
		}
		return style;
	}
	private static int borderWidth(BorderStyle border){
		int width=1;
		switch (border) {
		case NONE:
			width=1;
			break;
		case THIN:
			width=1;
			break;
		case MEDIUM:
			width=2;
			break;
		case DASHED:
			width=1;
			break;
		case DOTTED:
			width=1;
			break;
		case THICK:
			width=3;
			break;
		case DOUBLE:
			width=3;
			break;
		case HAIR:
			width=1;
			break;
		case MEDIUM_DASHED:
			width=2;
			break;
		case DASH_DOT:
			width=1;
			break;
		case MEDIUM_DASH_DOT:
			width=2;
			break;
		case DASH_DOT_DOT:
			width=1;
			break;
		case MEDIUM_DASH_DOT_DOT:
			width=2;
			break;
		case SLANTED_DASH_DOT:
			width=2;
			break;
		default:
			break;
		}
		return width;
	}
	public static String border(CellStyle topleft, CellStyle bottomright, Sheet sheet){
		if(bottomright==null){
			bottomright=topleft;
		}
		StringBuilder css=new StringBuilder();
		//left--------------------------------------------------------------------------------------

		if(topleft instanceof XSSFCellStyle && bottomright instanceof XSSFCellStyle){
			XSSFCellStyle xssfTopleft = (XSSFCellStyle)topleft;
			XSSFCellStyle xssfBottomright = (XSSFCellStyle)bottomright;
			String blcolor="#000";
			if(xssfTopleft.getBorderColor(BorderSide.LEFT)!=null){
				byte[] rgb=xssfTopleft.getBorderColor(BorderSide.LEFT).getRGBWithTint();
				if(rgb!=null) blcolor="rgb("+(rgb[0] & 0xff)+","+(rgb[1] & 0xff)+","+(rgb[2] & 0xff)+")";
			}
			css.append("border-left-color:").append(blcolor).append(";");
			css.append("border-left-style:").append(borderStyle(topleft.getBorderLeftEnum())).append(";");
			css.append("border-left-width:").append(borderWidth(topleft.getBorderLeftEnum())).append("px").append(";");
			//top---------------------------------------------------------------------------------------
			String btcolor="#000";
			if(xssfTopleft.getBorderColor(BorderSide.TOP)!=null){
				byte[] rgb=xssfTopleft.getBorderColor(BorderSide.TOP).getRGBWithTint();
				if(rgb!=null) btcolor="rgb("+(rgb[0] & 0xff)+","+(rgb[1] & 0xff)+","+(rgb[2] & 0xff)+")";
			}
			css.append("border-top-color:").append(btcolor).append(";");
			css.append("border-top-style:").append(borderStyle(topleft.getBorderTopEnum())).append(";");
			css.append("border-top-width:").append(borderWidth(topleft.getBorderTopEnum())).append("px").append(";");
			//right-------------------------------------------------------------------------------------
			String brcolor="#000";
			if(xssfBottomright.getBorderColor(BorderSide.RIGHT)!=null){
				byte[] rgb=xssfBottomright.getBorderColor(BorderSide.RIGHT).getRGBWithTint();
				if(rgb!=null) brcolor="rgb("+(rgb[0] & 0xff)+","+(rgb[1] & 0xff)+","+(rgb[2] & 0xff)+")";
			}
			css.append("border-right-color:").append(brcolor).append(";");
			css.append("border-right-style:").append(borderStyle(bottomright.getBorderRightEnum())).append(";");
			css.append("border-right-width:").append(borderWidth(bottomright.getBorderRightEnum())).append("px").append(";");
			//bottom------------------------------------------------------------------------------------
			String bbcolor="#000";
			if(xssfBottomright.getBorderColor(BorderSide.BOTTOM)!=null){
				byte[] rgb=xssfBottomright.getBorderColor(BorderSide.BOTTOM).getRGBWithTint();
				if(rgb!=null) bbcolor="rgb("+(rgb[0] & 0xff)+","+(rgb[1] & 0xff)+","+(rgb[2] & 0xff)+")";
			}
			css.append("border-bottom-color:").append(bbcolor).append(";");
			css.append("border-bottom-style:").append(borderStyle(bottomright.getBorderBottomEnum())).append(";");
			css.append("border-bottom-width:").append(borderWidth(bottomright.getBorderBottomEnum())).append("px").append(";");
		}else if(topleft instanceof HSSFCellStyle && bottomright instanceof HSSFCellStyle && sheet instanceof HSSFSheet){
			HSSFCellStyle hssfTopleft = (HSSFCellStyle)topleft;
			HSSFCellStyle hssfBottomright = (HSSFCellStyle)bottomright;
			//TODO wujh HexString 转成RBG
			HSSFPalette palette = ((HSSFWorkbook) sheet.getWorkbook()).getCustomPalette();
			HSSFColor hssfLeftBordercolor = palette.getColor(hssfTopleft.getLeftBorderColor());
			String blcolor="#000";
			if(hssfLeftBordercolor!=null){
				short[] rgb = hssfLeftBordercolor.getTriplet();
				if(rgb!=null) blcolor="rgb("+(rgb[0] & 0xff)+","+(rgb[1] & 0xff)+","+(rgb[2] & 0xff)+")";
			}
			css.append("border-left-color:").append(blcolor).append(";");
			css.append("border-left-style:").append(borderStyle(topleft.getBorderLeftEnum())).append(";");
			css.append("border-left-width:").append(borderWidth(topleft.getBorderLeftEnum())).append("px").append(";");
			//top---------------------------------------------------------------------------------------
			HSSFColor hssfTopBordercolor = palette.getColor(hssfTopleft.getTopBorderColor());
			String btcolor="#000";
			if(hssfTopBordercolor!=null){
				short[] rgb = hssfTopBordercolor.getTriplet();
				if(rgb!=null) btcolor="rgb("+(rgb[0] & 0xff)+","+(rgb[1] & 0xff)+","+(rgb[2] & 0xff)+")";
			}
			css.append("border-top-color:").append(btcolor).append(";");
			css.append("border-top-style:").append(borderStyle(topleft.getBorderTopEnum())).append(";");
			css.append("border-top-width:").append(borderWidth(topleft.getBorderTopEnum())).append("px").append(";");
			//right-------------------------------------------------------------------------------------
			HSSFColor hssfRightBordercolor = palette.getColor(hssfTopleft.getRightBorderColor());
			String brcolor="#000";
			if(hssfRightBordercolor!=null){
				short[] rgb = hssfRightBordercolor.getTriplet();
				if(rgb!=null) brcolor="rgb("+(rgb[0] & 0xff)+","+(rgb[1] & 0xff)+","+(rgb[2] & 0xff)+")";
			}
			css.append("border-right-color:").append(brcolor).append(";");
			css.append("border-right-style:").append(borderStyle(bottomright.getBorderRightEnum())).append(";");
			css.append("border-right-width:").append(borderWidth(bottomright.getBorderRightEnum())).append("px").append(";");
			//bottom------------------------------------------------------------------------------------
			HSSFColor hssfBottomBordercolor = palette.getColor(hssfTopleft.getBottomBorderColor());
			String bbcolor="#000";
			if(hssfBottomBordercolor!=null){
				short[] rgb = hssfBottomBordercolor.getTriplet();
				if(rgb!=null) bbcolor="rgb("+(rgb[0] & 0xff)+","+(rgb[1] & 0xff)+","+(rgb[2] & 0xff)+")";
			}
			css.append("border-bottom-color:").append(bbcolor).append(";");
			css.append("border-bottom-style:").append(borderStyle(bottomright.getBorderBottomEnum())).append(";");
			css.append("border-bottom-width:").append(borderWidth(bottomright.getBorderBottomEnum())).append("px").append(";");
		}
		return css.toString();
	}
}
